<template>
  <div class="login_container">
    <div class="login_box clearfix">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="">
      </div>
      <!-- 登录表单区域 -->
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" label-width="0px" class="login_form">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input v-model="loginForm.username">
            <template #prefix>
              <el-icon class="iconfont icon-user"><calendar /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" autocomplete="off">
            <template #prefix>
              <el-icon class="iconfont icon-3702mima"><calendar /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-form-item class="btns">
          <el-button type="primary" :loading="loading" @click="doLogin(loginFormRef)">登录</el-button>
          <el-button type="info" @click="resetLoginForm(loginFormRef)">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
 
<script setup lang="ts">
import { ElNotification } from 'element-plus';
import { ref, reactive } from 'vue'
import { useRouter, useRoute } from 'vue-router';
const loginFormRef = ref()
import useUserStore from "@/stores/modules/user";
let useStore = useUserStore();
//定义变量控制按钮加载效果
let loading = ref(false);
//获取路由器
let $router = useRouter();
//路由对象
let $route = useRoute();

const loginForm = reactive({
	username: 'admin',
	password: '1234561',
})
const loginFormRules = reactive({
    username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
    ],
    password:[
        { required:true,message:'请输入密码',trigger: 'blur'},
        { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
    ]
})
const resetLoginForm = formEl => {
    if (!formEl) return
    formEl.resetFields()
}
const doLogin = async formEl => {
  if (!formEl) return
  await formEl.validate();
  loading.value = true;
  try {
    //保证登录成功
    useStore.userLogin(loginForm);
    //登录成功提示信息
    ElNotification({
      type: 'success',
      message: '欢迎回来'
    });
    let redirect: any = $route.query.redirect;
    $router.push({ path: redirect || '/' });

    //登录成功加载效果也消失
    loading.value = false;
  } catch (error) {
    //登录失败加载效果消息
    loading.value = false;
    //登录失败的提示信息
    ElNotification({
      type: 'error',
      message: (error as Error).message
    })
  }
}
</script>
 
<style scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100vh;
}
.login_box {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  transform: translate(-50%, -50%);
}
.login_box .avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
}
.login_box .avatar_box img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eee;
}
.login_form {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;

}
.login_form .btns{
    float: right
}
</style>